//package com.ntd.frame.mybatis;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import com.ntd.marco.mybatis.mapper.TestMapper;
//import com.zaxxer.hikari.HikariConfig;
//import com.zaxxer.hikari.HikariDataSource;
//import org.apache.ibatis.io.Resources;
//import org.apache.ibatis.mapping.Environment;
//import org.apache.ibatis.session.Configuration;
//import org.apache.ibatis.session.SqlSession;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.ibatis.session.SqlSessionFactoryBuilder;
//import org.apache.ibatis.transaction.TransactionFactory;
//import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
//
//import javax.sql.DataSource;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.List;
//
//public class MybatisInit {
//
//    private List<Class> mappers = new ArrayList<>();
//
//    public static SqlSession init() {
//        SqlSessionFactory sqlSessionFactory = generateSqlSessionByDataSource(null);
//        return sqlSessionFactory.openSession();
//    }
//
//    public static void main(String[] args) throws IOException {
//        SqlSessionFactory sqlSessionFactory = generateSqlSessionByDataSource(null);
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        TestMapper mapper = sqlSession.getMapper(TestMapper.class);
//        mapper.selectBlog(2l);
//    }
//
//    /**
//     * @param dataSource
//     * @return
//     */
//    private static SqlSessionFactory generateSqlSessionByDataSource(DataSource dataSource) {
//        if (ObjectUtil.isNull(dataSource)) {
//            dataSource = generateDefaultDataSource();
//        }
//        TransactionFactory transactionFactory = new JdbcTransactionFactory();
//        Environment environment = new Environment("development", transactionFactory, dataSource);
//        Configuration configuration = new Configuration(environment);
////    configuration.addMapper(BlogMapper.class);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
//        return sqlSessionFactory;
//    }
//
//    /**
//     * @param resource
//     * @return
//     * @throws IOException
//     */
//    private static SqlSessionFactory generateSqlSessionByResourceXml(String resource) throws IOException {
//        if (StrUtil.isBlank(resource)) {
//            resource = "mybatis/mybatis-config.xml";
//        }
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        return sqlSessionFactory;
//    }
//
//    private static DataSource generateDefaultDataSource() {
//        DataSource dataSource;
//        HikariConfig hikariConfig = new HikariConfig();
//        hikariConfig.setJdbcUrl("jdbc:mysql://192.168.1.30:3306/wdcms?useUnicode=true&characterEncoding=utf8");
//        hikariConfig.setUsername("root");
//        hikariConfig.setPassword("root");
//        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        dataSource = new HikariDataSource(hikariConfig);
//        return dataSource;
//    }
//}
